﻿CloudDisk.Admin.SessionMgr = (function(){
    var session = null;
	var host = CloudDisk.Setting.API_HOST;
	var clientId = CloudDisk.Setting.CLIENT_ID;
	var clientSecret = CloudDisk.Setting.CLIENT_SECRET;
	var accessToken = '0bc6eef634889d131ec9a118d3a96e0d';
	
	return {
	    login: function(usrName, pwd){
			$.ajax({
				type: "POST",
				url: host + 'yxz/api/user/login',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
			    data: {
				    username: usrName,
					password: pwd,
					client_id: clientId,
					client_secret: clientSecret,
					response_type: 'token',
                    grant_type:'password',
                    device_type:'1',
                    device_name:'web',
                    device_info:'browser'
				},
				dataType: 'json',

				success: function(o) {
					accessToken = o.result.access_token;
				},
				error: function(data) {
				    alert(data.msg);
				}
			});	
		},
		
		querySpaceInfo: function(callback){
			$.ajax({
				type: "GET",
				url: host + 'yxz/api/user/spaceinfo',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				dataType: 'json'
			}).done(callback).error(function(data) {
				alert(data.msg);
			});			
		},
		
		queryUserList: function(callback){
			$.ajax({
				type: "GET",
				url: host + 'yxz/api/user/list',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				dataType: 'json'
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		updateUser: function(user, callback){
			$.ajax({
				type: "PUT",
				url: host + 'yxz/api/user/update/' + user.user_id,
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				data: {
					user_type: user.user_type,
					group_id: user.group_id,
				}
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		addUser: function(user, callback){
			$.ajax({
				type: "POST",
				url: host + 'yxz/api/user/create',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				data: user
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		deleteUser: function(user, callback){
			$.ajax({
				type: "DELETE",
				url: host + 'yxz/api/user/delete/' + user.user_id,
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		queryGroupList: function(callback){
			$.ajax({
				type: "GET",
				url: host + 'yxz/api/group/list',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				dataType: 'json'
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},		
		
		addGroup: function(group, callback){
			$.ajax({
				type: "POST",
				url: host + 'yxz/api/group/create',
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				data: group
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},		
		
		updateGroup: function(group, callback){
			$.ajax({
				type: "PUT",
				url: host + 'yxz/api/group/update/' + group.group_id,
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}, 
				data: group
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		deleteGroup: function(group, callback){
			$.ajax({
				type: "DELETE",
				url: host + 'yxz/api/group/delete/' + group.group_id,
				headers: {
				    'HTTP_X_OAUTH': accessToken
				}
			}).done(callback).error(function(data) {
				alert(data.msg);
			});				
		},
		
		logout: function(){}
	};
})();